<?php

// TTTTTTTTTTTTTTTTTTTTTTT                  MMMMMMMM               MMMMMMMM                                CCCCCCCCCCCCMMMMMMMM               MMMMMMMM  SSSSSSSSSSSSSSS 
// T:::::::::::::::::::::T                  M:::::::M             M:::::::M                             CCC::::::::::::M:::::::M             M:::::::MSS:::::::::::::::S
// T:::::::::::::::::::::T                  M::::::::M           M::::::::M                           CC:::::::::::::::M::::::::M           M::::::::S:::::SSSSSS::::::S
// T:::::TT:::::::TT:::::T                  M:::::::::M         M:::::::::M                          C:::::CCCCCCCC::::M:::::::::M         M:::::::::S:::::S     SSSSSSS
// TTTTTT  T:::::T  TTTTTppppp   ppppppppp  M::::::::::M       M::::::::::M   eeeeeeeeeeee          C:::::C       CCCCCM::::::::::M       M::::::::::S:::::S            
//         T:::::T       p::::ppp:::::::::p M:::::::::::M     M:::::::::::M ee::::::::::::ee       C:::::C             M:::::::::::M     M:::::::::::S:::::S            
//         T:::::T       p:::::::::::::::::pM:::::::M::::M   M::::M:::::::Me::::::eeeee:::::ee     C:::::C             M:::::::M::::M   M::::M:::::::MS::::SSSS         
//         T:::::T       pp::::::ppppp::::::M::::::M M::::M M::::M M::::::e::::::e     e:::::e     C:::::C             M::::::M M::::M M::::M M::::::M SS::::::SSSSS    
//         T:::::T        p:::::p     p:::::M::::::M  M::::M::::M  M::::::e:::::::eeeee::::::e     C:::::C             M::::::M  M::::M::::M  M::::::M   SSS::::::::SS  
//         T:::::T        p:::::p     p:::::M::::::M   M:::::::M   M::::::e:::::::::::::::::e      C:::::C             M::::::M   M:::::::M   M::::::M      SSSSSS::::S 
//         T:::::T        p:::::p     p:::::M::::::M    M:::::M    M::::::e::::::eeeeeeeeeee       C:::::C             M::::::M    M:::::M    M::::::M           S:::::S
//         T:::::T        p:::::p    p::::::M::::::M     MMMMM     M::::::e:::::::e                 C:::::C       CCCCCM::::::M     MMMMM     M::::::M           S:::::S
//       TT:::::::TT      p:::::ppppp:::::::M::::::M               M::::::e::::::::e                 C:::::CCCCCCCC::::M::::::M               M::::::SSSSSSS     S:::::S
//       T:::::::::T      p::::::::::::::::pM::::::M               M::::::Me::::::::eeeeeeee          CC:::::::::::::::M::::::M               M::::::S::::::SSSSSS:::::S
//       T:::::::::T      p::::::::::::::pp M::::::M               M::::::M ee:::::::::::::e            CCC::::::::::::M::::::M               M::::::S:::::::::::::::SS 
//       TTTTTTTTTTT      p::::::pppppppp   MMMMMMMM               MMMMMMMM   eeeeeeeeeeeeee               CCCCCCCCCCCCMMMMMMMM               MMMMMMMMSSSSSSSSSSSSSSS   
//                        p:::::p                                                                                                                                       
//                        p:::::p                                                                                                                                       
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       ppppppppp                                                                                                                                      

//  _____      __  __         ____ __  __ ____  
// |_   __ __ |  \/  | ___   / ___|  \/  / ___|     | AUTHOR: Xiaohe
//   | || '_ \| |\/| |/ _ \ | |   | |\/| \___ \     | EMAIL: 496631085@qq.com
//   | || |_) | |  | |  __/ | |___| |  | |___) |    | WECHAT: he4966
//   |_|| .__/|_|  |_|\___|  \____|_|  |_|____/     | DATETIME: 2023/03/20
//      |_|                                         | TpMeCMS

declare(strict_types=1);

namespace ImiApp\ApiServer\Api\Controller\store;

use Imi\Db\Db;
use Imi\Server\Http\Route\Annotation\Action;
use Imi\Server\Http\Route\Annotation\Controller;
use Imi\Server\Http\Route\Annotation\Route;
use Psr\Http\Message\ResponseInterface;
use Imi\Aop\Annotation\Inject;
// use Phpben\Imi\Validate\Annotation\Validate;
use Phpben\Imi\Auth\Annotation\Auth;
use Imi\Cache\CacheManager as Cache;
use Imi\Config;
use ImiApp\ApiServer\Api\Controller\Tpmecms;

/**
 * 店铺公共
 * @Auth(name="token",nologin={"*"})
 * @Controller("/store.store_com/")
 */
class StoreCom extends Tpmecms
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];


    /**
     * 获取店铺营业类型
     *
     * @param integer $page 1
     * @param integer $limit 50
     * @return void
     * @Action
     */
    public function get_storebusinesstype_list(?int $page = 1, ?int $limit = 50)
    {
        $page = $page ?: 1;
        $limit = $limit ?: 50;
        $list = Db::name('storebusinesstype')
            ->page($page, $limit)
            ->select()->getArray();
        return $this->success($page, $list);
    }

    // /**
    //  * 获取店铺类型
    //  *
    //  * @param integer $page 1
    //  * @param integer $limit 50
    //  * @return void
    //  * @Action
    //  */
    // public function get_storetype_list(?int $page = 1, ?int $limit = 50)
    // {
    //     $page = $page ?: 1;
    //     $limit = $limit ?: 50;
    //     $list = Db::name('storetype')
    //         ->page($page, $limit)
    //         ->select()->getArray();
    //     return $this->success($page, $list);
    // }


    /**
     * 获取自我声明
     *
     * @return void
     * @Action
     */
    public function get_self_declaration_list()
    {
        $list = Db::name('self_declaration')
            ->where('id', '>=', 1)
            ->select()->getArray();
        return $this->success('', $list);
    }


    /**
     * 获取店铺列表
     *
     * @param integer $page 1
     * @param integer $limit 5
     * @return void
     * @Action
     */
    public function get_store_list(?int $page = 1, ?int $limit = 5)
    {
        $page = $page ?: 1;
        $limit = $limit ?: 5;
        $where['status'] = '1'; //状态:0=关闭,1=正常
        $where['apply_status'] = '2'; //审核状态:0=未知,1=申请中,2=已通过,3=已拒绝
        $list = Db::name('store')->whereEx($where)->page($page, $limit)->select()->getArray();
        if ($list) {
            foreach ($list as $key => $val) {
            }
        }
        return $this->success('list', $list);
    }

    /**
     * 获取店铺信息
     *
     * @return void
     * @Action
     */
    public function get_store_data($id = null)
    {
        $store = Db::name('store')->where('id', '=', $id)->select()->get();
        if (!$store) {
            return $this->error('没有该店铺');
        }
        return $this->success('store', $store);
    }


    /**
     * 获取店铺名是否存在
     *
     * @param string $name 店铺名
     * @return void
     * @Action
     */
    public function get_store_name(?string $name = null)
    {
      
        if ($name){
            $where['name'] = [trim($name)];
        }else{
            return $this->error('请输入店铺名');
        }
            
        $id = Db::name('store')->whereEx($where)->value('id');
        return $this->success($name, $id);
    }
}
